package com.yuanlp.mobileplayer.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuanlp.mobileplayer.R;
import com.yuanlp.mobileplayer.bean.MediaItem;
import com.yuanlp.mobileplayer.utils.Utils;

import java.util.List;

/**
 * Created by 原立鹏 on 2017/7/16.
 */

public class MyAdapter extends BaseAdapter {

    private Context context;
    private List<MediaItem> mediaList;
    private Utils utils;
    private boolean isVideo;
    public MyAdapter(Context context,List<MediaItem> mediaList,boolean isVideo){
        this.context=context;
        this.mediaList=mediaList;
        this.isVideo=isVideo;
       utils=new Utils();

    }

    @Override
    public int getCount() {
        return mediaList.size();
    }

    @Override
    public Object getItem(int position) {
        return mediaList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView==null){
            View view = LayoutInflater.from(context).inflate(R.layout.medialayout, null);
            convertView=view;
            viewHolder=new ViewHolder();
            viewHolder.iv_icon= (ImageView) convertView.findViewById(R.id.iv_icon);
            viewHolder.tv_name= (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_time= (TextView) convertView.findViewById(R.id.tv_time);
            viewHolder.tv_size= (TextView) convertView.findViewById(R.id.tv_size);

            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }

        //得到数据
        MediaItem item=mediaList.get(position);
        viewHolder.tv_name.setText(item.getName());
        viewHolder.tv_size.setText(android.text.format.Formatter.formatFileSize(context,item.getSize()));
        viewHolder.tv_time.setText(utils.stringForTime((int) item.getDuration()));

        if (!isVideo){
            //来自音频，那么就把listview的图片换掉
            viewHolder.iv_icon.setImageResource(R.drawable.music_default_bg);
        }
        return convertView;
    }


    /**
     * 公共的控件类，里面包含每一行要显示的控件
     */
    private static class ViewHolder{
        ImageView iv_icon;
        TextView tv_name;
        TextView tv_time;
        TextView tv_size;
    }
}
